package com.bhs.sansonglogistics.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.CityBean;
import com.bhs.sansonglogistics.bean.CityDataBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.CityAdapter;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 修改专线的区
 */
public class CityPickerEditDialog extends Dialog implements View.OnClickListener, NetCallBack {

    private boolean isMultipleChoice;//是否多选
    private TextView mTvTitle;
    private TextView mTvCounty;
    private RecyclerView mRvProvince;
    private CityAdapter provinceAdapter;
    private List<CityBean> provinceList = new ArrayList<>();
    private RecyclerView mRvCity;
    private CityAdapter cityAdapter;
    private List<CityBean> cityList = new ArrayList<>();
    ;
    private RecyclerView mRvCounty;
    private CityAdapter countyAdapter;
    public List<CityBean> countyList = new ArrayList<>();
    private BaseActivity baseActivity;
    private String province, city, county = "";
    String[] countys;
    private int countIndex = 0;

    public CityPickerEditDialog(@NonNull BaseActivity context, boolean isMultipleChoice, String province, String city, String county) {
        super(context, R.style.MyBottomDialogStyle);
        this.baseActivity = context;
        this.isMultipleChoice = isMultipleChoice;
        this.province = province;
        this.city = city;
        this.county = county;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_city_picker);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = MyUtils.getScreenHeight(getContext()) * 7 / 10; //设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        initView();
        if (isMultipleChoice) {
            mTvTitle.setText("选择地址(多选)");
        }
        baseActivity.networkRequest(baseActivity.netApi.getCityData(), this);
        if (isMultipleChoice) {
            countys = county.split(",");
        }

        cityAdapter = new CityAdapter(cityList, 2, isMultipleChoice);
        mRvCity.setAdapter(cityAdapter);
        mRvCity.setLayoutManager(new LinearLayoutManager(baseActivity));

        countyAdapter = new CityAdapter(countyList, 3, isMultipleChoice);
        mRvCounty.setAdapter(countyAdapter);
        mRvCounty.setLayoutManager(new LinearLayoutManager(baseActivity));

        provinceAdapter = new CityAdapter(provinceList, 1, isMultipleChoice);
        mRvProvince.setAdapter(provinceAdapter);
        mRvProvince.setLayoutManager(new LinearLayoutManager(baseActivity));

        countyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!isMultipleChoice) {
                    countyList.get(countIndex).setCheck(false);
                    countyList.get(position).setCheck(true);
                    countIndex = position;
                } else {
                    if (countyList.get(position).getCity_name().equals("全市")) {
                        countyList.get(position).setCheck(!countyList.get(position).isCheck());
                        for (CityBean cityBean : countyList) {
                            cityBean.setCheck(countyList.get(position).isCheck());
                        }
                    } else {
                        countyList.get(position).setCheck(!countyList.get(position).isCheck());
                        boolean isHas = true;
                        for (CityBean cityBean : countyList) {
                            if (!cityBean.isCheck()) {
                                isHas = false;
                                break;
                            }
                        }
                        countyList.get(0).setCheck(isHas);
                    }
                }
                countyAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.tv_confirm) {
            if (myClickListener != null) {
                StringBuilder stringBuilder = new StringBuilder();

                for (CityBean cityBean : countyList) {
                    if (cityBean.isCheck()) {
                        if (isMultipleChoice) {
                            stringBuilder.append(cityBean.getCity_name());
                            stringBuilder.append(",");
                        } else {
                            stringBuilder.append(cityBean.getCity_name());
                            break;
                        }
                    }
                }
                if(TextUtils.isEmpty(stringBuilder)){
                    ToastUtil.show("必须选择一个区县");
                    return;
                }
                if (isMultipleChoice) {
                    stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
                }
                dismiss();
                myClickListener.onClick(null, stringBuilder.toString());
            }
        }else {
            dismiss();
        }
    }

    private void initView() {
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        mTvTitle = findViewById(R.id.tv_title);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        findViewById(R.id.et_search).setVisibility(View.GONE);
        mTvCounty = findViewById(R.id.tv_county);
        mRvProvince = findViewById(R.id.rv_province);
        mRvCity = findViewById(R.id.rv_city);
        mRvCounty = findViewById(R.id.rv_county);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        CityDataBean cityBean = new Gson().fromJson(json, CityDataBean.class);
        if (cityBean.isStatus()) {
            for (CityBean bean : cityBean.getData().getCity_china()) {
                if (bean.getCity_name().equals(province)) {
                    bean.setCheck(true);
                    provinceList.add(bean);
                    break;
                }
            }
            for (CityBean bean : provinceList.get(0).getChildren()) {
                if (bean.getCity_name().equals(city)) {
                    bean.setCheck(true);
                    cityList.add(bean);
                    break;
                }
            }
            if (isMultipleChoice) {
                countyList.add(new CityBean("全市"));
            }
            countyList.addAll(cityList.get(0).getChildren());
            for (int i = 0; i < countyList.size(); i++) {
                boolean isHas = false;
                if (!isMultipleChoice) {
                    if (countyList.get(i).getCity_name().equals(county)) {
                        countyList.get(i).setCheck(true);
                        countIndex = i;
                        break;
                    }
                } else {
                    for (String s : countys) {
                        if (countyList.get(i).getCity_name().equals(s)) {
                            isHas = true;
                            break;
                        }
                    }
                }
                countyList.get(i).setCheck(isHas);
            }

            provinceAdapter.notifyDataSetChanged();
            cityAdapter.notifyDataSetChanged();
            countyAdapter.notifyDataSetChanged();
        } else {
            ToastUtil.show(cityBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    private MyClickListener2 myClickListener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }
}
